﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ApplicationLOLS13
{
    class RandomUtil
    {
        private static int[] nums;
        private static Random rand = new Random();
        public static void Init(int[] indexArray)
        {
            nums = indexArray;
        }
        public static int[] Shuffle()
        {
            int n = nums.Length;
            int[] copy = new int[n];
            Array.Copy(nums, copy, n);
            for (int i = 0; i < n; i++)
            {
                // 生成一个 [i, n-1] 区间内的随机数
                int r = i + rand.Next(n - i);
                // 交换 nums[i] 和 nums[r],即获取第r个数【放到第i个位置】
                Swap(copy, i, r);
            }
            return copy;
        }

        /// <summary>
        /// 交换数组两个数的值
        /// </summary>
        /// <param name="nums"></param>
        /// <param name="i"></param>
        /// <param name="j"></param>
        private static void Swap(int[] nums, int i, int j)
        {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }
}
